<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/3/22
 * Time: 8:54
 */

namespace Common\Model;


use houdunwang\arr\Arr;

class GoodsModel extends BaseModel
{
    protected $pk        = 'gid';//主键
    protected $tableName = 'goods';//指定操作数据表
    protected $_validate = [
        ['gname','require','请输入商品名称'],
    ];
    //1.获取分类数据【树状结构】
    public function getCate()
    {
        //1.方法一 获取分类列表里面的所有数据及树状结构
        // $data =  m('category')->select();
        // return (new \Org\Util\Arr())->tree($data,'cname');
        //2.获取方法二 【简洁版】
        return   (new \Org\Util\Arr())->tree(m('category')->select(),'cname');
    }
    //2.获取品牌数据
    public function  getBrand()
    {
        //查找品牌里表里面的所有数据 return出去接收
        return  m('brand')->select();
    }
    public function store($data)
    {
        //验证商品表
        if(!$this->create()){
            return ['vaild'=>0,'msg'=>$this->getError()];
        }
        //验证商品详细表
        $detailModel = new DetailModel();
        if(!$detailModel->create()){
            return ['vaild'=>0,'msg'=>$detailModel->getError()];
        }
        if(!isset($data['img'])){
            return ['vaild'=>0,'msg'=>'请上传商品图册'];
        }
        //验证商品属性表
        if(!isset($data['attr']) || !isset($data['spec'])){
            return ['vaild'=>0,'msg'=>'请选择属性规格数据'];
        }
        //商品添加
        //压入时间
        $data['time'] = time();
        //压入管理员id
        $data['id'] =session('uid');
        // dd(session('uid'));die;
        //add方法是框架自带的添加方法
        $gid =  $this->add($data);
        //商品详细表添加
        $bigPath = '';//定义一个路径，储存上传的多图路径
        $spicPath ='';//中图
        $mpicPath ='';//小图
        foreach($data['img'] as $v){
            $image =  new \Think\Image();//实例化图像类
            $image->open($v);//打开一个图像
            //大图
            $big =dirname($v).'/big_'.basename($v);//拼一个新路径
            $bigPath .=$big.'|';//图片以|隔开
            $image->thumb(460,800)->save($big);
            //中图
            $spic =dirname($v).'/spic_'.basename($v);//拼一个新路径
            $spicPath .=$spic.'|';//图片以|隔开
            $image->thumb(460,800)->save($spic);
            //小图
            $mpic =dirname($v).'/mpic_'.basename($v);//拼一个新路径
            $mpicPath .=$mpic.'|';//图片以|隔开
            $image->thumb(200,200)->save($mpic);
        }
        $detailData = [
            'small'=>rtrim($mpicPath,'|'),//小图
            'medium'=>rtrim($spicPath,'|'),//中图
            'big'=>rtrim($bigPath,'|'),//大图
            'intro'=>$data['intro'],//商品详细
            'service'=>$data['service'],//售后服务
            'gid'=>$gid,//主键ID
        ];
        //dd($detailData);die;
        //执行添加方法
        $detailModel->add($detailData);
        //dd($data);die;
        //商品属性表添加
        $goodsAttrModel =  new GoodsAttrModel();
//        dd($goodsAttrModel);die;
        foreach($data['attr'] as $k=>$v){
            if($v){
                $goodsAttrDate =[
                    'gtvalue'=>$v,//商品属性值
                    'added'=>0,//附加价格
                    'taid'=>$k,//所属属性ID
                    'gid'=>$gid,//商品ID
                ];
                //dd($goodsAttrDate);die;
                $goodsAttrModel->add($goodsAttrDate);
            }

        }
        //循环规格属性 添加到数据库
        foreach($data['spec'] as $k=>$v)
        {
            foreach($v['spec'] as $kk=>$vv)
            {
                $goodsSpecDate =[
                    'gtvalue'=>$vv,
                    'added'=>$v['added'][$kk],
                    'taid'=>$k,
                    'gid'=>$gid,
                ];
                $goodsAttrModel->add($goodsSpecDate);

//                dd($goodsAttrDate);die;
            }

        }
        //走到这里说明，验证添加没有问题
        return ['valid'=>1,'msg'=>'操作成功'];
    }

    /**
     * 编辑方法
     *
     */
    public function edit($data,$gid)
    {
        //打印下看看有没有走进来
        //dd($data);
        //dd($gid);

        //验证商品表
        if(!$this->create()){
            return ['vaild'=>0,'msg'=>$this->getError()];
        }
        //验证商品详细表
        $detailModel = new DetailModel();
        if(!$detailModel->create()){
            return ['vaild'=>0,'msg'=>$detailModel->getError()];
        }
        if(!isset($data['img'])){
            return ['vaild'=>0,'msg'=>'请上传商品图册'];
        }
        //验证商品属性表
        if(!isset($data['attr']) || !isset($data['spec'])){
            return ['vaild'=>0,'msg'=>'请选择属性规格数据'];
        }
        //商品添加
        //压入时间
        $data['time'] = time();
        //压入管理员id
        $data['id'] =session('uid');
        // dd(session('uid'));die;
        //add方法是框架自带的添加方法
        $this->where("gid=$gid")->save($data);
        //商品详细表添加
        $bigPath = '';//定义一个路径，储存上传的多图路径
        $spicPath ='';//中图
        $mpicPath ='';//小图
        foreach($data['img'] as $v){
            $image =  new \Think\Image();//实例化图像类
            $image->open($v);//打开一个图像
            //大图
            $big =dirname($v).'/big_'.basename($v);//拼一个新路径
            $bigPath .=$big.'|';//图片以|隔开
            $image->thumb(1000,1000)->save($big);
            //中图
            $spic =dirname($v).'/spic_'.basename($v);//拼一个新路径
            $spicPath .=$spic.'|';//图片以|隔开
            $image->thumb(500,500)->save($spic);
            //小图
            $mpic =dirname($v).'/mpic_'.basename($v);//拼一个新路径
            $mpicPath .=$mpic.'|';//图片以|隔开
            $image->thumb(300,300)->save($mpic);
        }
        $detailData = [
            'small'=>rtrim($mpicPath,'|'),//小图
            'medium'=>rtrim($spicPath,'|'),//中图
            'big'=>rtrim($bigPath,'|'),//大图
            'intro'=>$data['intro'],//商品详细
            'service'=>$data['service'],//售后服务
            'gid'=>$gid,//主键ID
        ];
        //dd($detailData);die;
        //执行添加方法
        $detailModel->where("gid=$gid")->save($detailData);
        //dd($data);die;
        //商品属性表添加
        $goodsAttrModel =  new GoodsAttrModel();
//        dd($goodsAttrModel);die;
        //**********************************************************
        if($gid){
           m('goods_attr')->where("gid=$gid")->delete();
        }
//      m('goods_attr')->where(['gid'=>$gid])->delete();
        foreach($data['attr'] as $k=>$v){
            if($v){
                $goodsAttrDate =[
                    'gtvalue'=>$v,//商品属性值
                    'added'=>0,//附加价格
                    'taid'=>$k,//所属属性ID
                    'gid'=>$gid,//商品ID
                ];
                $goodsAttrModel->add($goodsAttrDate);
            }

        }
        //循环规格属性 添加到数据库
        foreach($data['spec'] as $k=>$v)
        {
            foreach($v['spec'] as $kk=>$vv)
            {
                $goodsSpecDate =[
                    'gtvalue'=>$vv,
                    'added'=>$v['added'][$kk],
                    'taid'=>$k,
                    'gid'=>$gid,
                ];
                $goodsAttrModel->where("gid=$gid")->add($goodsSpecDate);

//                dd($goodsAttrDate);die;
            }

        }
        //走到这里说明，验证添加没有问题
        return ['valid'=>1,'msg'=>'编辑成功'];

    }
    public function del($gid)
    {

//        m('goods')->where("gid=$gid")->delete();
//        m('detail')->where("gid=$gid")->delete();
//        m('goods_attr')->where("gid=$gid")->delete();

    }

}